//
//  WaterfallLayout.m
//  TTLayoutDemo
//
//  Created by 余意 on 2016/12/13.
//  Copyright © 2016年 赵春浩. All rights reserved.
//

#import "WaterfallLayout.h"

@implementation WaterfallLayout

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.lineNum = 10;
        self.rowSpace = 10.f;
        self.lineSpace = 10.f;
        self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _heightDic = [NSMutableDictionary new];
        _array = [NSMutableArray new];
    }
    return self;
}

#pragma mark - 布局时调用
- (void)prepareLayout
{
    [super prepareLayout];
    
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    //初始化每列的高度
    for (NSInteger i = 0 ; i < self.lineNum ; i ++)
    {
        [_heightDic setObject:@(self.sectionInset.top) forKey:[NSString stringWithFormat:@"%ld",i]];
    }
    
    //初始化每个item的属性值进行存储
    for (NSInteger i = 0 ; i < count ; i ++)
    {
        NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [_array addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
    }
    
}

#pragma mark - 可滚动区域的size
- (CGSize)collectionViewContentSize
{
    __block NSString * maxHeightline = @"0";
    [_heightDic enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSNumber * obj,BOOL * stop)
    {
        if ([_heightDic[maxHeightline] floatValue] < [obj floatValue])
        {
            maxHeightline = key;
        }
    }];
    return CGSizeMake(self.collectionView.bounds.size.width, [_heightDic[maxHeightline] floatValue] + self.sectionInset.bottom);
}

#pragma mark - 返回视图框内item的属性，可以直接返回所有item属性
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return _array;
}

#pragma mark - 计算indexPath下item 属性的方法
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes * attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    //计算item的宽 (总宽度 - 左右宽度 - 中间宽度)/ 列数
    CGFloat itemW = (self.collectionView.bounds.size.width - (self.sectionInset.left + self.sectionInset.right + (self.lineNum - 1) * self.lineSpace ))/ self.lineNum;
    
    //计算item的高
    CGFloat itemH ;
    if (self.heightblock != nil)
    {
        itemH = self.heightblock(indexPath,itemW);
    }
    else
    {
        NSAssert(itemH != 0,@"Please implement computeIndexCellHeightWithWidthBlock Method");
    }
    
    //计算item的frame
    CGRect frame ;
    frame.size = CGSizeMake(itemW, itemH);
    
    //遍历找出高度最短行
    __block NSString * lineMinHeight = @"0";
    [_heightDic enumerateKeysAndObjectsUsingBlock:^(NSString * key,NSNumber * obj, BOOL * stop)
    {
        if ([_heightDic[lineMinHeight] floatValue] > [obj floatValue])
        {
            lineMinHeight = key;
        }
    }];
    
    int line = [lineMinHeight intValue];
    
    //找出最短行，计算出item的位置
    frame.origin = CGPointMake(self.sectionInset.left + line * (itemW + self.lineSpace), [_heightDic[lineMinHeight] floatValue]);
    _heightDic[lineMinHeight] = @(frame.size.height + self.rowSpace + [_heightDic[lineMinHeight] floatValue]);
    attr.frame = frame;
    
    /*
    if (indexPath.item == 99)
    {
        CGRect rect = attr.frame;
        rect.origin.y += 10;
        attr.frame = rect;
    }
     */
    
    return attr;
}

#pragma mark - 计算item高度的block
- (void)computeIndexCellHeightWithWidthBlock:(CGFloat (^)(NSIndexPath *, CGFloat))block
{
    if (self.heightblock != block)
    {
        self.heightblock = block;
    }
}


@end
